#!/bin/sh

# $XConsortium: startx.cpp,v 1.4 91/08/22 11:41:29 rws Exp $
# $XFree86: xc/programs/xinit/startx.cpp,v 3.5 2000/12/02 18:06:58 herrb Exp $
# 
# This is just a sample implementation of a slightly less primitive 
# interface than xinit.  It looks for user .xinitrc and .xserverrc
# files, then system xinitrc and xserverrc files, else lets xinit choose
# its default.  The system xinitrc should probably do things like check
# for .Xresources files and merge them in, startup up a window manager,
# and pop a clock and serveral xterms.
#
# Site administrators are STRONGLY urged to write nicer versions.
# 

#
# check for installed servers
#
INSTALLED_SERVERS=`ls -1 /usr/X11R6/bin/Xsun* /usr/X11R6/bin/XFree86 /usr/X11R6/bin/XF86_* /usr/X11R6/bin/Xaccel* 		   /usr/X11R6/bin/XSuSE* /usr/X11R6/bin/Xmetr* /usr/X11R6/bin/XFCom* /usr/X11R6/bin/XBF* 2>/dev/null`


if [ -z "$INSTALLED_SERVERS" ]; then

	echo ""
	echo "You did not install any X-Server \(e.g. XF86_SVGA\)!"
	echo "Please install at least one server to start X."
	echo "I'm aborting now."
	exit 1
fi

# 
#  now check if Accelerated X is installed as that may have problems
#  with gziped fonts
# 
case "$INSTALLED_SERVERS" in
  /usr/X11R6/bin/Xaccel*)
    FONTLIST=`echo "/usr/X11R6/lib/X11/fonts/*/*.gz"`
    for FONT in $FONTLIST; do
        if test -f $FONT ; then
            echo "ATTENTION!!! Found AcceleratedX Server - probably it can not"
            echo "handle Fonts compressed with gzip."
            echo
            echo "If you have troubles, please try"
            echo
            echo "  for DIR in /usr/X11R6/lib/X11/fonts/* ; do"
            echo "      cd \$DIR || continue"
            echo "      for FILE in *.gz ; do"
            echo "          BASE=\`basename \$FILE .gz\`"
            echo "          gunzip -f \$FILE"
            echo "          compress \$BASE"
            echo "      done"
            echo "      mkfontdir ."
            echo "  done"
            echo
            sleep 5
        fi
        break
    done
    ;;
  *)
    ;;
esac


# 
#  perhaps a windowmanager is specified?
# 
#
# find wmlist utility, else take twm as default (always there :-))
#
type -p wmlist &> /dev/null && WMLIST="`for i in $(wmlist); do echo $i; done | sort`"
test -z "$WMLIST" && WMLIST="twm"

test "$1" = "--" || { test -n "$1" && WANTEDWM=`basename "$1"` ; }

#
# User could have set a list of window manager in WINDOWMANAGERLIST.
# go throu wms and check if user specified a reachable one.
# 
for WM in $WMLIST $WINDOWMANAGERLIST
do 
    test "$WM" = "$WANTEDWM" && {
	#
	# is it reachable via $PATH?
	#
	type -p $WM &> /dev/null || {

	    echo "`basename $0`: error: the requested window manager '$1' could not be found!"
	    echo "Window manager must be one of (currently installed window managers):"
	    for i in $WMLIST; do echo $i; done
	    exit 1
	}
	#
	# OK, we got it, this will be the override for WINDOWMANAGER
	#
	export WINDOWMANAGER=$WM
	shift 1
	break
    }
done

unset WINDOWMANAGERLIST WMLIST WANTEDWM WM

#
# check for the first link
#

if [ ! -L /usr/X11R6/bin/X ]; then

	echo ""
	echo "There is no link /usr/X11R6/bin/X to /var/X11R6/bin/X!"
	echo "Please link the files as mentioned above or install the X-Server again."
	echo "I'm aborting now."
	exit 1
fi

#
# check for the second link
# 	

if [ ! -L /var/X11R6/bin/X ]; then

	echo ""
	echo "There is no link /var/X11R6/bin/X to the correct X Server binary."
	echo "Please configure the correct X Server with SaX/SaX2, which will create"
	echo "the missing link. I'm aborting now."
	exit 1
fi

#
# check if the XF86_xxxx file is an executable or an accidentially copied script or similar things
#

if [ ! -x /var/X11R6/bin/X ]; then

	echo ""
	echo "The file XF86_xxxx (binary of X-Server) doesn't seem to be a binary file."
	echo "Please check it or / and install a new X-Server-binary."
	echo "I'm aborting now."
	exit 1
fi

userclientrc=$HOME/.xinitrc
userserverrc=$HOME/.xserverrc
sysclientrc=/usr/X11R6/lib/X11/xinit/xinitrc
sysserverrc=/usr/X11R6/lib/X11/xinit/xserverrc
clientargs=""
serverargs=""

if [ -f $userclientrc ]; then
    clientargs=$userclientrc
elif [ -f $sysclientrc ]; then
    clientargs=$sysclientrc
fi

if [ -f $userserverrc ]; then
    serverargs=$userserverrc
elif [ -f $sysserverrc ]; then
    serverargs=$sysserverrc
fi

display=:0
whoseargs="client"
while [ "x$1" != "x" ]; do
    case "$1" in
	/''*|\.*)
		if [ "$whoseargs" = "client" ]; then
		    if [ -f "$1" ]; then
			clientargs="$1"
		    else
			clientargs="$clientargs $1"
		    fi
		else
		    if [ -f "$1" ]; then
			serverargs="$1"
		    else
			serverargs="$serverargs $1"
		    fi
		fi ;;
	--)	whoseargs="server" ;;
	*)	if [ "$whoseargs" = "client" ]; then
		    clientargs="$clientargs $1"
		else
		    case "$1" in
			:[0-9]*) display="$1"; serverargs="$serverargs $1";;
			*) serverargs="$serverargs $1" ;;
		    esac
		fi ;;
    esac
    shift
done

# set up default Xauth info for this machine
mcookie=`mcookie`
if [ X"$XAUTHORITY" = X ]; then
    authfile="$HOME/.Xauthority"
else
    authfile="$XAUTHORITY"
fi
serverargs="$serverargs -auth $authfile"
xauth -f $authfile source - <<-EOF
	add $display . $mcookie
	add `hostname -f`$display . $mcookie
	add `hostname -f`/unix$display . $mcookie
EOF
mcookie=

xinit $clientargs -- $serverargs
